package fido.torrentsite;


import fido.DefaultTorrentImpl;
import fido.FidoCommonUtils;
import org.mymediadb.api.mmdb.model.Movie;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;


public final class TorrentLeech extends CookiedTorrentSite {

    protected String getRegex() {
        return "<tr class=\\\"(even|odd)\\\".*?id=(.*?)>.*?<a href=\\\".*?<a href=\\\"(.*?)\\\">(.*?)</a>.*?<a href=\\\"(.*?)\\\">.*?seeders\\\">(.*?)</td>.*?leechers\\\">.*?(.*?)</td>";
    }

    protected String getURL() {
        return "http://www.torrentleech.org";
    }

    protected String getSearchURL() {
        return "http://www.torrentleech.org/torrents/browse/index/query/{0}/categories/13/orderby/seeders/order/desc";
    }

    protected boolean isLoggedIn(String loginPageUrl) {
        return (!loginPageUrl.contains("loginFormBlock"));
    }

    protected String getTextEncoding() {
        return "UTF-8";
    }

    protected List<CookieVariable> getCookieVariables() {
        List<CookieVariable> cookieVariables = new ArrayList<CookieVariable>();
        cookieVariables.add(new CookieVariable("tluid", "user id"));
        cookieVariables.add(new CookieVariable("tlpass", "passkey"));
        return cookieVariables;
    }

    protected DefaultTorrentImpl getTorrentFromMatcherData(Matcher matcher, Movie movie) {
        DefaultTorrentImpl torrent;
        String extraDetailsUrl;
        int torrentCounter = 0;

        while (matcher.find()) {
            torrentCounter++;
            try {
                torrent = new DefaultTorrentImpl();
                torrent.setDetailsPageUrl("http://www.torrentleech.org/torrents/torrent/nfotext?torrentID=" + matcher.group(2));
                extraDetailsUrl = "http://www.torrentleech.org" + matcher.group(3);
                torrent.setName(matcher.group(4));
                torrent.setTorrentUrl("http://www.torrentleech.org" + matcher.group(5));
                torrent.setSeeders(Integer.parseInt(matcher.group(6)));
                torrent.setLeechers(Integer.parseInt(matcher.group(7)));
                torrent.setCookie(getCookieString());
                if (FidoCommonUtils.doesImdbIdExistsInTorrentDetails(torrent, movie.getImdbId())) { //checking the NFO file for torrentleech
                    LOG.debug("Found torrent IMDBId in NFO file");
                    return torrent;
                }
                torrent.setDetailsPageUrl(extraDetailsUrl);   //This is the correct page
                if (FidoCommonUtils.doesImdbIdExistsInTorrentDetails(torrent, movie.getImdbId())) { //
                    LOG.debug("Found torrent IMDBId in details Page");
                    return torrent;
                }
                LOG.debug("Found torrent:" + torrent.getName() + " but imdbID didn't match");

            } catch (IndexOutOfBoundsException e) {
                LOG.info(e.toString());
                return null;
            }

        }

        if (torrentCounter == 0) {
            LOG.debug("No torrents found at all, There were no results on the search page?");
        } else {
            LOG.debug("Found " + torrentCounter + " torrents matching search name - but not ImdbID");
        }

        return null;
    }


}
